from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
import uuid


def get_uuid():
    return str(uuid.uuid4()).upper()


class CustomUserManager(BaseUserManager):
    """用户默认数据修改"""

    def create_user(self, username, password=None, **extra_fields):
        if not username:
            raise ValueError('The username field must be set')
        username = self.normalize_email(username)
        user = self.model(username=username, id=get_uuid(), **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        return self.create_user(username, password, **extra_fields)


class ClassificationModel(models.Model):
    """用户管理分类"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=64, unique=True, verbose_name='Name')
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '用户分类'
        verbose_name_plural = verbose_name


class ImageModel(models.Model):
    """图片上传接口"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    image = models.ImageField(upload_to='images/images')

    class Meta:
        verbose_name = '图片上传接口'
        verbose_name_plural = verbose_name


class AdmDivCodeModel(models.Model):
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    code = models.CharField(max_length=64, verbose_name='区号编码')
    name = models.CharField(max_length=64, verbose_name='名称')
    pid = models.CharField(max_length=64, verbose_name='pid', null=True, blank=True)
    level = models.CharField(max_length=64, verbose_name='level', null=True, blank=True)
    leaf = models.CharField(max_length=64, verbose_name='leaf', null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '区划管理'
        verbose_name_plural = verbose_name


class User(AbstractUser, PermissionsMixin):
    """用户管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    username = models.CharField(max_length=100, verbose_name='用户名', unique=True)
    nick_name = models.CharField(max_length=100, verbose_name='用户昵称')
    password = models.CharField(max_length=32, verbose_name='用户密码')
    admdivcode = models.ForeignKey(to='AdmDivCodeModel', on_delete=models.CASCADE, verbose_name='区划', default=None,
                                   blank=True, null=True)
    institution = models.ForeignKey(to='InstitutionsModel', verbose_name='所在机构', on_delete=models.CASCADE,
                                    null=True, blank=True, default=None)
    classification = models.ForeignKey(to='ClassificationModel', verbose_name='用户管理分类', on_delete=models.CASCADE,
                                       blank=True, null=True, default=None)
    email = models.CharField(max_length=100, verbose_name='邮箱', null=True, blank=True)
    phone = models.CharField(max_length=100, verbose_name='手机号', null=True, blank=True)
    state = models.CharField(max_length=100, verbose_name='状态', null=True, blank=True)
    data = models.JSONField(default=dict, verbose_name='其他数据', null=True, blank=True)
    modules = models.ManyToManyField(to='ModulesModel', verbose_name='应用管理', blank=True)
    role = models.ManyToManyField(to='RoleModel', verbose_name='角色管理', blank=True)
    is_InstitutionsType = models.ManyToManyField(verbose_name='岗位类型', to='InstitutionsTypesModel', blank=True)
    function = models.ManyToManyField(verbose_name='功能权限', to='FunctionModel', blank=True)
    # dataPermissions = models.ManyToManyField(verbose_name='功能权限', to='FunctionModel', null=True) # 数据权限
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    is_active = models.BooleanField(default=True, null=True, blank=True)
    is_staff = models.BooleanField(default=False, null=True, blank=True)
    objects = CustomUserManager()
    USERNAME_FIELD = 'username'

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '用户管理'
        verbose_name_plural = verbose_name


class ModulesModel(models.Model):
    """模块管理."""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    code = models.CharField(max_length=100, verbose_name='应用编码')
    name = models.CharField(max_length=100, verbose_name='应用名称')
    describe = models.CharField(max_length=300, verbose_name="应用描述")
    icon = models.CharField(max_length=300, verbose_name='图片地址', blank=True, null=True)
    sort = models.IntegerField(default=0, verbose_name="应用排序")
    is_active = models.BooleanField(default=False)
    data = models.JSONField(default=dict, verbose_name="其他参数")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '模块管理'
        verbose_name_plural = verbose_name


class MenusModel(models.Model):
    """功能集管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    module = models.ForeignKey(verbose_name='所属应用', to='ModulesModel', on_delete=models.CASCADE, default=None)
    name = models.CharField(max_length=100, verbose_name='功能集名称')
    describe = models.CharField(max_length=300, verbose_name="功能集描述")
    sort = models.IntegerField(default=0, verbose_name="排序")
    parent_id = models.IntegerField(default=0, verbose_name='父节点')
    router_path = models.CharField(max_length=500, verbose_name='路由地址')
    is_active = models.BooleanField(default=False)
    data = models.JSONField(default=dict, verbose_name="其他参数")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '功能集管理'
        verbose_name_plural = verbose_name


class FunctionModel(models.Model):
    """功能管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    module = models.ForeignKey(verbose_name='所属功能集', to='MenusModel', on_delete=models.CASCADE, default=None)
    name = models.CharField(max_length=100, verbose_name='名称')
    describe = models.CharField(max_length=300, verbose_name="描述")
    sort = models.IntegerField(default=0, verbose_name="排序")
    router_path = models.CharField(max_length=500, verbose_name='路由地址')
    is_active = models.BooleanField(default=False)
    data = models.JSONField(default=dict, verbose_name="其他参数")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '功能管理'
        verbose_name_plural = verbose_name


class RoleModel(models.Model):
    """角色管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    module = models.ForeignKey(verbose_name='所属功能集', to='MenusModel', on_delete=models.CASCADE, default=None)
    name = models.CharField(max_length=100, verbose_name='名称')
    describe = models.CharField(max_length=300, verbose_name="描述")
    sort = models.IntegerField(default=0, verbose_name="排序")
    router_path = models.CharField(max_length=500, verbose_name='路由地址')
    is_active = models.BooleanField(default=False)
    data = models.JSONField(default=dict, verbose_name="其他参数")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '功能管理'
        verbose_name_plural = verbose_name


class TablesModel(models.Model):
    """表管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    details = models.CharField(max_length=100)
    is_active = models.BooleanField()
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '表管理'
        verbose_name_plural = verbose_name


class FieldsModel(models.Model):
    """表字段管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    code = models.CharField(max_length=16)
    name = models.CharField(max_length=100)
    details = models.CharField(max_length=100)
    types = models.CharField(max_length=16)
    length = models.IntegerField()
    is_active = models.BooleanField()
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '表字段管理'
        verbose_name_plural = verbose_name


# 表生成
class TablesToModel(models.Model):
    """表生成"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    tables = models.ManyToManyField(TablesModel)
    fields = models.ManyToManyField(FieldsModel)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.id

    class Meta:
        verbose_name = '表生成'
        verbose_name_plural = verbose_name


class PostModel(models.Model):
    """岗位"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    details = models.CharField(max_length=100, default=None, null=True)
    is_active = models.BooleanField(default=True)
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '岗位'
        verbose_name_plural = verbose_name


class InstitutionsModel(models.Model):
    """机构管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    admdivcode = models.ForeignKey(verbose_name='行政区划编码', to='AdmDivCodeModel', on_delete=models.CASCADE,
                                   default=None, blank=True, null=True)
    code = models.CharField(verbose_name='机构编码', max_length=16, default=None, blank=True, null=True)
    name = models.CharField(verbose_name='机构名称', max_length=100)
    state = models.IntegerField(verbose_name='机构类型', default=0, choices=((1, "单位"), (2, "处室")))
    details = models.CharField(verbose_name='描述', max_length=100, default=None, null=True)
    is_active = models.BooleanField(verbose_name='状态', default=True)
    leaf = models.IntegerField(verbose_name='叶', default=0)
    level = models.IntegerField(verbose_name='排序', default=0)
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    is_type = models.ManyToManyField(to='InstitutionsTypesModel', blank=True)

    class Meta:
        verbose_name = '机构管理'
        verbose_name_plural = verbose_name


class InstitutionsTypesModel(models.Model):
    """机构类型配置"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(verbose_name='类型名', max_length=100)
    module = models.ForeignKey(verbose_name='所属应用', to='ModulesModel', on_delete=models.CASCADE, default=None)
    details = models.CharField(verbose_name='描述', max_length=100, default=None, null=True)
    is_active = models.BooleanField(verbose_name='状态', default=True)
    data = models.JSONField(verbose_name='参数', default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '机构类型'
        verbose_name_plural = verbose_name


class GropModel(models.Model):
    """权限组  """
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    details = models.CharField(max_length=100, default=None, null=True)
    is_active = models.BooleanField(default=True)
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '组'
        verbose_name_plural = verbose_name


class UserGropModel(models.Model):
    """用户组"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    is_active = models.BooleanField(default=True)
    data = models.JSONField(default=dict)
    user = models.ForeignKey(to="User", on_delete=models.CASCADE, default=None)
    group = models.ForeignKey(to='GropModel', on_delete=models.CASCADE, default=None)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '用户组'
        verbose_name_plural = verbose_name


class InstitutionsSelectModel(models.Model):
    """机构类型选择"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    details = models.CharField(max_length=100, default=None, null=True)
    is_active = models.BooleanField(default=True)
    left = models.IntegerField(default=0)
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '机构类型选择'
        verbose_name_plural = verbose_name


class MenusPermissionsModel(models.Model):
    """菜单权限管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    is_active = models.BooleanField()
    data = models.JSONField(default=dict)
    grop = models.ForeignKey(to='GropModel', on_delete=models.CASCADE, default=None)
    menu = models.ForeignKey(to='MenusModel', on_delete=models.CASCADE, default=None)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '权限管理'
        verbose_name_plural = verbose_name


class FlowModel(models.Model):
    """流程管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    details = models.CharField(max_length=100, default=None, null=True)
    is_active = models.BooleanField(default=True)
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '流程管理'
        verbose_name_plural = verbose_name


class ButtonsModel(models.Model):
    """按钮管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    is_active = models.BooleanField()
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '按钮管理'
        verbose_name_plural = verbose_name


class InterfaceModel(models.Model):
    """界面管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    is_active = models.BooleanField()
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '界面管理'
        verbose_name_plural = verbose_name


class ButtonsPermissionsModel(models.Model):
    """按钮权限管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    buttons = models.ForeignKey(MenusPermissionsModel, on_delete=models.CASCADE)
    is_active = models.BooleanField()
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '按钮权限管理'
        verbose_name_plural = verbose_name


class InterfacePermissionsModel(models.Model):
    """界面权限管理"""
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=100)
    is_active = models.BooleanField()
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '界面权限管理'
        verbose_name_plural = verbose_name


# 数据库链接

class DataBaseModel(models.Model):
    id = models.CharField(max_length=64, primary_key=True, verbose_name='UUID')
    name = models.CharField(max_length=32, verbose_name='数据库名')
    describe = models.CharField(max_length=32, verbose_name='数据库名', null=True, blank=True)
    state_choice = ((0, "Oracle"), (1, "Mysql"), (2, "PostgreSQL"))
    state = models.IntegerField(choices=state_choice, default=0)
    port = models.IntegerField(verbose_name='数据库端口')
    user_name = models.CharField(max_length=32, verbose_name='数据库用户名')
    password = models.CharField(max_length=32, verbose_name='数据库密码')
    data = models.JSONField(default=dict)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = '数据库管理'
        verbose_name_plural = verbose_name
